// Copyright 2022 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
//    contributors may be used to endorse or promote products derived from
//    this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package expectations

import (
	"testing"

	"dawn.googlesource.com/dawn/tools/src/cts/query"
	"dawn.googlesource.com/dawn/tools/src/cts/result"
	"github.com/stretchr/testify/assert"
)

var Q = query.Parse

/*******************************************************************************
 * removeExpectationsForUnknownTests tests
 ******************************************************************************/

// Tests that expectations for unknown tests are properly removed, even if they
// are in an immutable chunk.
func TestRemoveExpectationsForUnknownTests(t *testing.T) {
	startingContent := `
# BEGIN TAG HEADER
# OS
# tags: [ android linux mac win10 ]
# END TAG HEADER

# Partially removed, immutable.
[ android ] * [ Failure ]
[ mac ] a:* [ Failure ]
[ linux ] a:b,c:d,e:f=* [ Failure ]
[ linux ] valid_test1 [ Failure ]
[ linux ] invalid_test [ Failure ]
[ linux ] valid_test2 [ Failure ]

# Fully removed, immutable.
[ android ] invalid_test [ Failure ]
[ android ] c:* [ Failure ]

# Partially removed, mutable.
# ##ROLLER_AUTOGENERATED_FAILURES##
[ win10 ] valid_test1 [ Failure ]
[ win10 ] invalid_test [ Failure ]
[ win10 ] valid_test2 [ Failure ]
`

	content, err := Parse("expectations.txt", startingContent)
	assert.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	knownTests := []query.Query{
		query.Parse("valid_test1"),
		query.Parse("valid_test2"),
		query.Parse("a:b"),
		query.Parse("a:b,c:d,e:f=g"),
	}

	content.removeExpectationsForUnknownTests(&knownTests)

	expectedContent := `# BEGIN TAG HEADER
# OS
# tags: [ android linux mac win10 ]
# END TAG HEADER

# Partially removed, immutable.
[ android ] * [ Failure ]
[ mac ] a:* [ Failure ]
[ linux ] a:b,c:d,e:f=* [ Failure ]
[ linux ] valid_test1 [ Failure ]
[ linux ] valid_test2 [ Failure ]

# Partially removed, mutable.
# ##ROLLER_AUTOGENERATED_FAILURES##
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]
`

	assert.Equal(t, expectedContent, content.String())
}

/*******************************************************************************
 * removeUnknownTags tests
 ******************************************************************************/

// Tests that unknown tags are properly removed.
func TestRemoveUnknownTags(t *testing.T) {
	header := `# BEGIN TAG HEADER
# OS
# tags: [ android linux win10 ]
# END TAG HEADER
`

	content, err := Parse("expectations.txt", header)
	assert.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("android", "android-14", "arm"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("win", "win10", "intel", "intel-gen-9", "intel-0x3e9b"),
			Status: result.Failure,
		},
	}

	content.removeUnknownTags(&resultList)

	expectedList := result.List{
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("android"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("win10"),
			Status: result.Failure,
		},
	}

	assert.Equal(t, expectedList, resultList)
}

/*******************************************************************************
 * reduceTagsToMostExplicitOnly tests
 ******************************************************************************/

// Tests that result tags are properly filtered to only include the most
// explicit tags based on the tag sets in the header.
func TestReduceTagsToMostExplicitOnly(t *testing.T) {
	header := `
# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER
`

	content, err := Parse("expectations.txt", header)
	assert.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("android", "android-14", "arm"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("win", "win10", "intel", "intel-gen-9", "intel-0x3e9b"),
			Status: result.Failure,
		},
	}

	content.reduceTagsToMostExplicitOnly(&resultList)

	expectedList := result.List{
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("android-14", "arm"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("win10", "intel-0x3e9b"),
			Status: result.Failure,
		},
	}

	assert.Equal(t, expectedList, resultList)
}

/*******************************************************************************
 * addExpectationsToMutableChunk tests
 ******************************************************************************/

// Tests that multiple mutable chunks result in an error.
func TestAddExpectationsToMutableChunkMultipleMutableChunks(t *testing.T) {
	fileContent := `
# BEGIN TAG HEADER
# OS
# tags: [ android linux win10 ]
# END TAG HEADER

# ##ROLLER_AUTOGENERATED_FAILURES##
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]

# ##ROLLER_AUTOGENERATED_FAILURES##
[ linux ] valid_test1 [ Failure ]
[ linux ] valid_test2 [ Failure ]
`

	content, err := Parse("expectations.txt", fileContent)
	assert.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("android-14", "arm"),
			Status: result.Failure,
		},
	}

	err = content.addExpectationsToMutableChunk(&resultList)
	assert.EqualError(t, err, "Expected 1 mutable chunk, found 2")
}

// Tests that a single mutable chunk gets expectations added to it.
func TestAddExpectationsToMutableChunkSingleMutableChunk(t *testing.T) {
	fileContent := `
# BEGIN TAG HEADER
# OS
# tags: [ android linux win10 ]
# END TAG HEADER

# ##ROLLER_AUTOGENERATED_FAILURES##
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]
`

	content, err := Parse("expectations.txt", fileContent)
	assert.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("a:b,c:d"),
			Tags:   result.NewTags("android-14", "arm"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("z:b,c:d,e:f=g"),
			Tags:   result.NewTags("android-14", "arm"),
			Status: result.Failure,
		},
	}

	err = content.addExpectationsToMutableChunk(&resultList)
	assert.NoErrorf(t, err, "Failed to add expectations: %v", err)

	expectedContent := `# BEGIN TAG HEADER
# OS
# tags: [ android linux win10 ]
# END TAG HEADER

# ##ROLLER_AUTOGENERATED_FAILURES##
crbug.com/0000 [ android-14 arm ] a:b,c:d: [ Failure ]
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]
crbug.com/0000 [ android-14 arm ] z:b,c:d,e:f=g [ Failure ]
`

	assert.Equal(t, expectedContent, content.String())
}

// Tests that a lack of a mutable chunk causes a new mutable chunk to be added
// which has the new expectations.
func TestAddExpectationsToMutableChunkNoMutableChunk(t *testing.T) {
	fileContent := `
# BEGIN TAG HEADER
# OS
# tags: [ android linux win10 ]
# END TAG HEADER

# Immutable
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]
`

	content, err := Parse("expectations.txt", fileContent)
	assert.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("a:b,c:d"),
			Tags:   result.NewTags("android-14", "arm"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("z:b,c:d,e:f=g"),
			Tags:   result.NewTags("android-14", "arm"),
			Status: result.Failure,
		},
	}

	err = content.addExpectationsToMutableChunk(&resultList)
	assert.NoErrorf(t, err, "Failed to add expectations: %v", err)

	expectedContent := `# BEGIN TAG HEADER
# OS
# tags: [ android linux win10 ]
# END TAG HEADER

# Immutable
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]

################################################################################
# Autogenerated Failure expectations. Please triage.
# ##ROLLER_AUTOGENERATED_FAILURES##
################################################################################
crbug.com/0000 [ android-14 arm ] a:b,c:d: [ Failure ]
crbug.com/0000 [ android-14 arm ] z:b,c:d,e:f=g [ Failure ]
`

	assert.Equal(t, expectedContent, content.String())
}

/*******************************************************************************
 * AddExpectationsForFailingResults tests
 ******************************************************************************/

// Tests overall behavior with an existing mutable chunk.
func TestAddExpectationsForFailingResultsExistingChunk(t *testing.T) {
	fileContent := `
# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER

# Partially removed, immutable.
[ android ] * [ Failure ]
[ mac ] a:* [ Failure ]
[ linux ] z:b,c:d,e:f=* [ Failure ]
[ linux ] valid_test1 [ Failure ]
[ linux ] invalid_test [ Failure ]
[ linux ] valid_test2 [ Failure ]

# Fully removed, immutable.
[ android ] invalid_test [ Failure ]
[ android ] c:* [ Failure ]

# Partially removed, mutable.
# ##ROLLER_AUTOGENERATED_FAILURES##
[ win10 ] valid_test1 [ Failure ]
[ win10 ] invalid_test [ Failure ]
[ win10 ] valid_test2 [ Failure ]
`

	content, err := Parse("expectations.txt", fileContent)
	assert.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	knownTests := []query.Query{
		query.Parse("valid_test1"),
		query.Parse("valid_test2"),
		query.Parse("a:b,c:d:"),
		query.Parse("z:b,c:d,e:f=g"),
	}

	resultList := result.List{
		result.Result{
			Query:  query.Parse("a:b,c:d"),
			Tags:   result.NewTags("android", "android-14", "arm", "unknown1"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("z:b,c:d,e:f=g"),
			Tags:   result.NewTags("android", "android-14", "arm", "unknown2"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-9", "intel-0x3e9b", "unknown3"),
			Status: result.Failure,
		},
		// This should be merged with the one above after only the most explicit
		// tags remain. Multiple generations mapping to the same actual GPU
		// shouldn't happen in real life, but serves to test merging.
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-12", "intel-0x3e9b", "unknown4"),
			Status: result.Failure,
		},
	}

	err = content.AddExpectationsForFailingResults(resultList, knownTests, false)
	assert.NoErrorf(t, err, "Failed to add expectations: %v", err)

	expectedContent := `# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER

# Partially removed, immutable.
[ android ] * [ Failure ]
[ mac ] a:* [ Failure ]
[ linux ] z:b,c:d,e:f=* [ Failure ]
[ linux ] valid_test1 [ Failure ]
[ linux ] valid_test2 [ Failure ]

# Partially removed, mutable.
# ##ROLLER_AUTOGENERATED_FAILURES##
crbug.com/0000 [ android-14 arm ] a:b,c:d: [ Failure ]
crbug.com/0000 [ intel-0x3e9b linux ] valid_test1 [ Failure ]
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]
crbug.com/0000 [ android-14 arm ] z:b,c:d,e:f=g [ Failure ]
`

	assert.Equal(t, expectedContent, content.String())
}

// Tests overall behavior without an existing mutable chunk.
func TestAddExpectationsForFailingResultsWithoutExistingChunk(t *testing.T) {
	fileContent := `
# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER

# Partially removed, immutable.
[ android ] * [ Failure ]
[ mac ] a:* [ Failure ]
[ linux ] z:b,c:d,e:f=* [ Failure ]
[ linux ] valid_test1 [ Failure ]
[ linux ] invalid_test [ Failure ]
[ linux ] valid_test2 [ Failure ]

# Fully removed, immutable.
[ android ] invalid_test [ Failure ]
[ android ] c:* [ Failure ]
`

	content, err := Parse("expectations.txt", fileContent)
	assert.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	knownTests := []query.Query{
		query.Parse("valid_test1"),
		query.Parse("valid_test2"),
		query.Parse("a:b,c:d:"),
		query.Parse("z:b,c:d,e:f=g"),
	}

	resultList := result.List{
		result.Result{
			Query:  query.Parse("a:b,c:d"),
			Tags:   result.NewTags("android", "android-14", "arm", "unknown1"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("z:b,c:d,e:f=g"),
			Tags:   result.NewTags("android", "android-14", "arm", "unknown2"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-9", "intel-0x3e9b", "unknown3"),
			Status: result.Failure,
		},
		// This should be merged with the one above after only the most explicit
		// tags remain. Multiple generations mapping to the same actual GPU
		// shouldn't happen in real life, but serves to test merging.
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-12", "intel-0x3e9b", "unknown4"),
			Status: result.Failure,
		},
	}

	err = content.AddExpectationsForFailingResults(resultList, knownTests, false)
	assert.NoErrorf(t, err, "Failed to add expectations: %v", err)

	expectedContent := `# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER

# Partially removed, immutable.
[ android ] * [ Failure ]
[ mac ] a:* [ Failure ]
[ linux ] z:b,c:d,e:f=* [ Failure ]
[ linux ] valid_test1 [ Failure ]
[ linux ] valid_test2 [ Failure ]

################################################################################
# Autogenerated Failure expectations. Please triage.
# ##ROLLER_AUTOGENERATED_FAILURES##
################################################################################
crbug.com/0000 [ android-14 arm ] a:b,c:d: [ Failure ]
crbug.com/0000 [ intel-0x3e9b linux ] valid_test1 [ Failure ]
crbug.com/0000 [ android-14 arm ] z:b,c:d,e:f=g [ Failure ]
`

	assert.Equal(t, expectedContent, content.String())
}

// Tests overall behavior with multiple mutable chunks.
func TestAddExpectationsForFailingResultsMultipleExistingChunk(t *testing.T) {
	fileContent := `
# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER

# Partially removed, immutable.
[ linux ] valid_test1 [ Failure ]
[ linux ] invalid_test [ Failure ]
[ linux ] valid_test2 [ Failure ]

# Fully removed, immutable.
[ android ] invalid_test [ Failure ]

# Partially removed, mutable.
# ##ROLLER_AUTOGENERATED_FAILURES##
[ win10 ] valid_test1 [ Failure ]
[ win10 ] invalid_test [ Failure ]
[ win10 ] valid_test2 [ Failure ]

# ##ROLLER_AUTOGENERATED_FAILURES##
[ android ] valid_test1 [ Failure ]
[ android ] valid_test2 [ Failure ]
`

	content, err := Parse("expectations.txt", fileContent)
	assert.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	knownTests := []query.Query{
		query.Parse("valid_test1"),
		query.Parse("valid_test2"),
		query.Parse("new_test"),
		query.Parse("z_new_test"),
	}

	resultList := result.List{
		result.Result{
			Query:  query.Parse("new_test"),
			Tags:   result.NewTags("android", "android-14", "arm", "unknown1"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("z_new_test"),
			Tags:   result.NewTags("android", "android-14", "arm", "unknown2"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-9", "intel-0x3e9b", "unknown3"),
			Status: result.Failure,
		},
		// This should be merged with the one above after only the most explicit
		// tags remain. Multiple generations mapping to the same actual GPU
		// shouldn't happen in real life, but serves to test merging.
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-12", "intel-0x3e9b", "unknown4"),
			Status: result.Failure,
		},
	}

	err = content.AddExpectationsForFailingResults(resultList, knownTests, false)
	assert.EqualError(t, err, "Expected 1 mutable chunk, found 2")
}
